<template>
  <div class="detail-container">
    <div class="back"><span class="icon-arrow_lift" @click="close"></span></div>
    <div class="row" v-if="food">
      <div class="box_1">
        <img :src="food.image" alt="">
        <h1 class="name">{{ food.name }}</h1>
        <div class="sell_1"><span>月售{{ food.sellCount }}</span><span>好评率{{ food.rating }}%</span></div>
        <div class="sell_2"><span>￥</span><span class="now">{{ food.price }}</span><span class="old" v-if="food.oldPrice">￥{{
            food.oldPrice }}</span><span class="car" @click="add(food.price, food.name)" ref="car">加入购物车</span></div>
      </div>
      <div class="box_2">
        <h1 class="name">商品介绍</h1>
        <span v-if="food.info">{{ food.info }}</span>
        <span v-else>暂无</span>
      </div>
      <div class="box_3">
        <h1 class="name">商品评价</h1>
        <div class="btn">
          <div class="btn_1">全部</div>
          <div class="btn_2">推荐</div>
          <div class="btn_3">吐槽</div>
        </div>
        <div class="line"></div>
        <div class="ratingsbox">
          <div class="ratings" v-for="item in food.ratings" :key="item.username">
            <div class="ratings_1">
              <div class="time">{{ item.rateTime | timeFormat }}</div>
              <div class="info"><span :class="[item.rateType === 0 ? 'icon-thumb_up' : 'icon-thumb_down']"></span>&nbsp;&nbsp;<span
                  v-if="item.text">{{ item.text }}</span><span v-else>该用户没有评论</span></div>
            </div>
            <div class="ratings_2">
              <img :src="item.avatar" alt="头像">
              <span class="user">{{ item.username }}</span>
            </div>
          </div>

          <!-- <div class="ratings">
            <div class="ratings_1">
              <div class="time">2016</div>
              <div class="info"><span class="icon-thumb_up"></span><span>太少了太少了太少了太少了太少了太少了太少了太少了太少了太111</span></div>
            </div>
            <div class="ratings_2">
              <img src="http://static.galileo.xiaojukeji.com/static/tms/default_header.png" alt="">
              <span class="user">111</span>
            </div>
          </div> -->

        </div>
      </div>
    </div>
    <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
      <div class="ball" v-show="ballFlag"></div>
    </transition>
  </div>
</template>

<script>
const ERR_OK = 0
export default {
  name: 'DetailView',
  data () {
    return {
      allgoods: [],
      foods: [],
      food: {},
      ballFlag: false,
      end: {}
    }
  },
  created () {
    this.getInfo()
  },
  methods: {
    getInfo () {
      this.$http.get('http://localhost:8081/api/goods').then(res => {
        // console.log(res.body)
        if (!res.body.errno === ERR_OK) return
        this.allgoods = res.body.data
      })
    },
    checkcate (val) {
      // console.log(this.goodsmsg)
      // console.log(this.catesmsg)
      return val.name === this.catemsg
    },
    checkgoods (val) {
      return val.name === this.goodsmsg
    },
    close () {
      this.$emit('goodsfunc')
    },
    add (price, name) {
      this.$store.commit('add', {
        price,
        name
      })
      const badgePosition = document.getElementById('badge').getBoundingClientRect()
      const xEnd = badgePosition.right
      const yEnd = badgePosition.top
      this.end = {
        xEnd,
        yEnd
      }
      // console.log(this.badge)
      this.ballFlag = true
    },
    beforeEnter (el) {
      // console.log(this.$refs.car.getBoundingClientRect().left)
      // console.log(this.$refs.car.getBoundingClientRect().top)
      el.style.transform =
          `translate(${this.$refs.car.getBoundingClientRect().left}px, ${this.$refs.car.getBoundingClientRect().top}px)`
      // el.style.transform = 'translate(283px,51px)'
    },
    enter (el, done) {
      // 关闭下一行的校验 不然eslint会报错
      // eslint-disable-next-line
      el.offsetWidth
      // console.log('相对于ball(0,0)的yDist:' + this.end.xDist)
      // console.log('相对于ball(0,0)的yDist:' + this.end.yDist)
      el.style.transform = `translate(${this.end.xEnd}px, ${this.end.yEnd}px)`
      el.style.transition = 'all .5s cubic-bezier(.1, .67, .83, .67)'
      done()
    },
    afterEnter (el) {
      this.ballFlag = false
    }
  },
  props: ['catemsg', 'goodsmsg'],
  watch: {
    'catemsg': function (newVal, oldVal) {
      // console.log('新：' + newVal)
      // console.log('旧：' + oldVal)
      if (newVal === '') return
      // console.log(this.allgoods.find(this.checkcate).foods)
      this.foods = this.allgoods.find(this.checkcate).foods
    },
    'goodsmsg': function (newVal, oldVal) {
      // console.log('新：' + newVal)
      // console.log('旧：' + oldVal)
      if (newVal === '') return
      // console.log(this.foods.find(this.checkgoods))
      this.food = this.foods.find(this.checkgoods)
    }
  },
  filters: {
    timeFormat (time) {
      var date = new Date(time)
      return date.toLocaleDateString().replace(/\//g, '.') + date.toLocaleTimeString()
    }
  }
}

</script>

<style lang="scss" scoped>
  .detail-container {
    padding-top: 1rem;
    padding-bottom: 1.2rem;
    width: 100%;
    height: 100%;
    overflow: scroll;
    background-color: #fff;
    z-index: 100;

    .ball {
      // fixed相对于屏幕，因为用getBoundingClientRect()也是相对于屏幕
      position: fixed;
      top: 0;
      left: 0;
      width: 15px;
      height: 15px;
      border-radius: 50%;
      background-color: red;
      z-index: 99;
    }

    .name {
      height: 0.4rem;
      line-height: 0.4rem;
      font-size: 0.4rem;
      color: rgb(7, 17, 27);
    }

    .line {
      border-bottom: 1px solid rgba(7, 17, 27, .1);
    }

    .back {
      z-index: 10;
      position: fixed;
      top: 0;
      left: 0;
      height: 1rem;
      width: 100%;
      background-color: rgba(7, 17, 27, .85);

      .icon-arrow_lift {
        display: inline-block;
        padding-left: 0.2rem;
        width: 1rem;
        line-height: 1rem;
        font-size: 0.5rem;
        color: #fff;
      }

    }

    .row {
      background-color: rgba(147, 153, 159, .1);

      .box_1 {
        margin-bottom: 0.32rem;
        background-color: #fff;
        border-bottom: 1px solid rgba(7, 17, 27, .2);
        // border-top: 1px solid rgba(7, 17, 27, .2);

        img {
          display: block;
          width: 100%;
          height: 7.5rem;
        }

        h1 {
          margin-top: 0.36rem;
          margin-left: 0.36rem;
        }

        .sell_1 {
          padding: 0.16rem 0.36rem 0 0.36rem;
          height: 0.3rem;

          span {
            line-height: 0.3rem;
            color: rgb(147, 153, 159);
            font-size: 0.3rem
          }

          span:last-child {
            padding-left: 0.24rem;
          }
        }

        .sell_2 {
          padding: 0.36rem;
          width: 100%;
          height: 1.2rem;
          position: relative;

          span {
            line-height: 0.48rem;
            font-weight: normal;
            font-size: 0.3rem;
            color: rgb(240, 20, 20);
          }

          .now {
            font-size: 0.38rem;
            font-weight: 700;
          }

          .old {
            margin-left: 0.2rem;
            text-decoration: line-through;
            color: rgb(147, 153, 159);
          }

          .car {
            position: absolute;
            top: 0.36rem;
            right: 0.36rem;
            display: inline-block;
            width: 1.48rem;
            height: 0.48rem;
            line-height: 0.48rem;
            text-align: center;
            font-size: 0.2rem;
            color: #fff;
            background-color: rgb(0, 160, 220);
            border-radius: 24px;
          }
        }
      }

      .box_2 {
        margin-bottom: 0.32rem;
        padding: 0.36rem;
        background-color: #fff;
        border-bottom: 1px solid rgba(7, 17, 27, .2);
        border-top: 1px solid rgba(7, 17, 27, .2);

        span {
          display: inline-block;
          padding: 0.12rem 0.16rem 0 0.16rem;
          line-height: 0.48rem;
          font-size: 0.28rem;
          font-weight: 200;
          color: rgb(77, 85, 93);
        }
      }

      .box_3 {
        padding: 0.36rem;
        background-color: #fff;
        border-bottom: 1px solid rgba(7, 17, 27, .2);
        border-top: 1px solid rgba(7, 17, 27, .2);

        .btn {
          display: flex;
          align-items: center;
          padding: 0.36rem 0;
          width: 100%;
          height: 1.36rem;
          text-align: center;
          font-size: 0.3rem;

          div {
            padding: 0 0.24rem;
            margin-right: 0.16rem;
            height: 0.64rem;
            line-height: 0.64rem;
            border-radius: 2px;
          }

          .btn_1 {
            color: #fff;
            background-color: rgb(0, 160, 220);
          }

          .btn_2 {
            color: rgb(77, 85, 93);
            background-color: rgba(0, 160, 220, .2);
          }

          .btn_3 {
            color: rgb(77, 85, 93);
            background-color: rgba(77, 85, 93, .2);
          }
        }

        .ratingsbox {
          .ratings {
            padding: 0.32rem 0;
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-bottom: 1px solid rgba(7, 17, 27, .1);

            .ratings_1 {
              flex: 0 0 70%;
              height: 1.6rem;
              display: flex;
              flex-direction: column;
              justify-content: start;
              justify-content: space-around;

              .time {
                height: 0.3rem;
                line-height: 0.3rem;
                color: rgb(147, 153, 159);
              }

              .info {
                margin-top: 0.2rem;
                height: 1.1rem;
                line-height: 0.55rem;
                font-size: 0.32rem;
                color: rgb(7, 17, 27);
                vertical-align: middle;
                overflow: hidden;
                display: -webkit-box;
                -webkit-box-orient: vertical;
                -webkit-line-clamp: 2;
                text-overflow: ellipsis;

                .icon-thumb_down {
                  color: rgb(147, 153, 159);
                }

                .icon-thumb_up {
                  color: rgb(0, 160, 220);
                }
              }
            }

            .ratings_2 {
              flex: 0 0 30%;
              display: flex;
              flex-direction: column;
              align-items: center;

              img {
                width: 1rem;
                height: 1rem;
              }

              .user {
                display: inline-block;
                margin-top: 0.2rem;
                height: 0.4rem;
                line-height: 0.4rem;
              }
            }
          }
        }
      }
    }
  }

</style>
